defmodule SyntaxMacro do
    @moduledoc false

    import ErrCode, [:unexpected_error, :input_error]

    defmacro begin(do: block) do
        quote do: unquote(block)
    end

    defmacro judge_do(do: block) do
        quote do
            case unquote(block) do
                {:ok, resp} ->
                    resp
                :ok ->
                    :ok
                {:error, err} ->
                    throw({:error, err})
                err ->
                    throw({:error, err, unexpected_error()})
            end
        end
    end


    defmacro throw_error(error, reason \\ nil) do
        quote bind_quoted: [
                  reason: reason,
                  err: error
              ] do
            if reason == nil do
                throw({:error, err})
            else
                throw({:error, reason, err})
            end
        end
    end

    def remind(env) do
        m = env.module
        l = env.line
        throw_error(inspect(m) <> "_input_error_" <> inspect(l))
    end

    def debug_remind(env, reason) do
        case AppConfigure.is_dev() do
            true ->
                m = env.module
                l = env.line
                throw({:error, "mod: #{m}, line: #{l}, #{reason}"})
            _ ->
                throw({:error, input_error()})
        end
    end

    def convert_list(value) do
        [value]
    end

    def append2(value, list) when is_list(value) do
        value ++ list
    end
    def append2(nil, list) do
        list
    end
    def append2(value, list) do
        [value | list]
    end

    defmacro iif?(bool, is_block, else_block) do
        quote do
            if unquote(bool) == true do
                unquote(is_block)
            else
                unquote(else_block)
            end
        end
    end

    defmacro not_null(value) do
        quote do
            if unquote(value) == nil do
                throw({:error, input_error()})
            else
                unquote(value)
            end
        end
    end




    defmacro __using__(_options) do
        quote do
            import unquote(__MODULE__)
        end
    end


end
